import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { overdueStatisticsAction } from './index';
import { CommonTable } from 'components';
import moment from 'moment';
import SearchList from './SearchList/SearchList';


class OverdueStatistics extends Component {

    columns = [
        { title: '逾期时间', dataIndex: 'day', key: 'day' },
        { title: '逾期单量', dataIndex: 'num', key: 'num' },
        { title: '逾期正常应还金额', dataIndex: 'amount', key: 'amount' },
        { title: '在催单量', dataIndex: 'ingNum', key: 'ingNum' },
        { title: '在催金额', dataIndex: 'ingMoney', key: 'ingMoney' },
        { title: '催回正常应还金额', dataIndex: 'backMoney', key: 'backMoney' },
        // { title: '催回展期费用', dataIndex: 'lengMoney', key: 'lengMoney' },
        { title: '催回滞纳金', dataIndex: 'overMoney', key: 'overMoney' },
        { title: '总回款', dataIndex: 'totalAmount', key: 'totalAmount' },
        {
            title: '回单率',
            dataIndex: 'backRate',
            key: 'backRate',
            render(text) {
                const data = Number(text) * 100;
                return `${data.toFixed(2)}%`;
            }
        },
        {
            title: '回款率',
            dataIndex: 'id',
            key: 'id',
            render(text, record) {
                const { amount, totalAmount } = record;
                const result = Number(totalAmount) / Number(amount) * 100;
                return Number.isInteger(result) ? `${result}%` : `${result.toFixed(2)}%`
            }
        }
    ];

    constructor(props) {
        super(props);
        this.state = {};
        this.initTime = [
            moment().subtract(7, 'days'),
            moment()
        ];
    }

    convertParams = (obj = {}) => {
        const { time = this.initTime, days = '' } = obj;
        const isArr = Array.isArray(time) && time.length > 0;
        return {
            startTime: isArr ? time[0].format('YYYY-MM-DD 00:00:00'): '',
            endTime: isArr? time[1].format('YYYY-MM-DD 23:59:59') : '',
            days
        };
    }

    handleSearch = (obj) => {
        const params = this.convertParams(obj);
        const { getTableData } = this.props;
        getTableData(params);
    }
    componentDidMount() {
        const { getTableData } = this.props;
        getTableData(this.convertParams());
    }

    componentWillUnmount() {
        const { setTableData } = this.props;
        setTableData([]);
    }


    render() {
        const { tableData, loading } = this.props;
        return (
            <div>
                <SearchList initTime={this.initTime} handleSearch={this.handleSearch}/>
                <CommonTable dataSource={tableData} loading={loading} pagination={null} columns={this.columns}/>
            </div>
        );
    }


}

const mapStateToProps = (state) => {
    const { businessStatisticsState: { overdueStatisticsState } } = state;
    return {
        tableData: overdueStatisticsState['tableData'],
        loading: overdueStatisticsState['loading']
    };
}
const mapDispatchToProps = (dispatch) => {
    return bindActionCreators({
        getTableData: overdueStatisticsAction.oscGetTableData,
        setTableData: overdueStatisticsAction.oscSetTableData
    }, dispatch);
}


export default connect(mapStateToProps, mapDispatchToProps)(OverdueStatistics);